package renda.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.lang.xwork.StringUtils;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import renda.dao.BaseDAO;
import renda.dao.TopicsDAO;
import renda.pojo.Topics;

@Repository("topicsDAO")
@Transactional
public class TopicsDAOImpl extends BaseDAO implements TopicsDAO {

	@Transactional(propagation = Propagation.REQUIRED)
	public Topics saveTopics(Topics topics) {
		/* Serializable s = */
		getTemplate().save(topics);
		// System.out.println("s = " + s);
		// topics.setTid((Integer) s);
		return topics;
	}

	@SuppressWarnings("unchecked")
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public List<Topics> listTopics(final String name, final Integer userId, final int offset, final int length) {
		List executeFind = getTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				StringBuilder hql = new StringBuilder("from Topics t");
				StringBuilder where = new StringBuilder();
				if (StringUtils.isNotBlank(name)) {
					where.append(" where t.topicname like '%").append(name.trim()).append("%'");
				}
				if (userId != null) {
					if (StringUtils.isNotBlank(where.toString())) {
						where.append(" and t.uid = ").append(userId).append(" ");
					} else {
						where.append(" where t.uid = ").append(userId).append(" ");
					}
				}
				hql.append(where.toString()).append(" order by t.tid desc");
				// System.out.println("hql = " + hql.toString());
				Query query = session.createQuery(hql.toString());
				// System.out.println("query = " + query);
				query.setFirstResult(offset);
				query.setMaxResults(length);
				List list2 = query.list();
				return list2;
			}
		});
		List<Topics> topics = executeFind;
		return topics;
	}

	@SuppressWarnings("unchecked")
	public Topics getById(Integer topicsId) {
		List list = getTemplate().find("from Topics where tid = ?", topicsId == null ? -1 : topicsId);
		if (list != null && list.size() > 0) {
			return (Topics) list.get(0);
		}
		return null;
	}

	public int countTopics(String name, Integer userId) {
		StringBuilder hql = new StringBuilder("select count(t.tid) from Topics t");
		StringBuilder where = new StringBuilder();
		if (StringUtils.isNotBlank(name)) {
			where.append(" where t.topicname like '%").append(name.trim()).append("%'");
		}
		if (userId != null) {
			if (StringUtils.isNotBlank(where.toString())) {
				where.append(" and t.uid = ").append(userId);
			} else {
				where.append(" where t.uid = ").append(userId);
			}
		}
		hql.append(where.toString());
		System.out.println("hql = " + hql.toString());
		Long count = (Long) getTemplate().find(hql.toString()).listIterator().next();
		count = count == null ? 0L : count;
		return count.intValue();
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void updateCount(Integer topicsId, boolean isUser) {
		if (topicsId != null && topicsId > 0) {
			Topics topics = getById(topicsId);
			if (topics != null) {
				if (isUser) {
					topics.setIsuserreturn(topics.getIsuserreturn() + 1);
				} else {
					topics.setIsadminreturn(topics.getIsadminreturn() + 1);
				}
				getTemplate().update(topics);
			}
		}
	}

}
